package com.example.demo01.demo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.demo01.R;
import com.example.demo01.adapter.GridDemoAdapter;
import com.example.demo01.consts.UserData;
import com.example.demo01.entity.UserDemo;
import com.example.demo01.fragments.TitleFragment;
import com.example.demo01.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class GridViewDemo extends AppCompatActivity {
    ToastUtil toastUtil = new ToastUtil(this);
    private GridView gv_grid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid_view);
        EdgeToEdge.enable(this);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        //. 添加标题
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.fl_title, TitleFragment.newInstance(getResources().getString(R.string.GridViewDemo)))
                .commit();

        initView();
    }

    //. 初始化视图
    private void initView() {
        //. 初始化控件
        gv_grid = findViewById(R.id.gv_grid);


        addListener();
    }

    //. 添加点击事件
    private void addListener() {
        //. 创建适配器
        GridDemoAdapter Adapter = new GridDemoAdapter(this, UserData.USER_DEMOS);
        //. 设置适配器
        gv_grid.setAdapter(Adapter);
        //. 设置点击事件
        gv_grid.setOnItemClickListener((parent, view, position, id) -> {
            Intent intent = new Intent(GridViewDemo.this, IntentTestPage.class);
            intent.putExtra("user", Adapter.getItem(position));
            startActivity(intent);
        });

    }
}